<?php

class FaceBookLoginModel extends CoreModel {

	function __construct()
	{
		parent::__construct();
	}

	public function returnTable()
	{
		return $this->FBLogin;
	}

	public function topUserPracticeTest()
	{
		return $this->wpdb->get_results("SELECT * FROM ".$this->returnTable()." ORDER BY accumulation_point DESC LIMIT 15");
	}

	public function userExists($id)
	{
		$q = $this->wpdb->get_var("SELECT user_id FROM ".$this->returnTable()." WHERE user_id='".$id."'");
		return !empty($q) ? true : false;
	}

	public function getStatus($id)
	{
		$q = $this->wpdb->get_var("SELECT isLogin FROM ".$this->returnTable()." WHERE user_id='".$id."'");
		return ($q==1) ? true : false;
	}

	public function setStatus($id)
	{
		if($this->getStatus($id) == false && $this->userExists($id)){
			$this->wpdb->update($this->returnTable(),array('isLogin'=>1),array('user_id'=>$id));
			return true;
		}else{
			return false;
		}
	}

	public function updateStatus($id)
	{
		if($this->getStatus($id) == true && $this->userExists($id)){
			$this->wpdb->update($this->returnTable(),array('isLogin'=>0),array('user_id'=>$id));
			return true;
		}else{
			return false;
		}
	}

	public function getLoginCount($id)
	{
		return $this->wpdb->get_var("SELECT login_count FROM ".$this->returnTable()." WHERE user_id='".$id."'");
	}

	public function getUserProfile($id)
	{
		if(!empty($id)){
			return $this->wpdb->get_row("SELECT * FROM ".$this->returnTable()." WHERE user_id='".$id."' AND isLogin=1");
		}else{
			return '';
		}
	}

	public function deleteUser($id)
	{
		return $this->wpdb->delete($this->returnTable(),array('user_id'=>$id));
	}

	public function blacklistUser($id)
	{
		return $this->wpdb->update($this->returnTable(),array('is_blacklist'=>1),array('user_id'=>$id));
	}

	public function runActionAdminTableView($action,$id)
	{
		switch ($action) {
	        case 'delete':
	        	if(is_array($id)){
	        		foreach($id as $value){
	        			$this->deleteUser($value);
	        		}
	        	}else{
	        		$this->deleteUser($id);
	        	}
	            break;
	        case 'blacklist':
	        	if(is_array($id)){
	        		foreach($id as $value){
	        			$this->blacklistUser($value);
	        		}
	        	}else{
	        		$this->blacklistUser($id);
	        	}
	            break;
	    }
	}

}